echo ====== DO NOT RELEASE DOWNLOAD KEY UNTIL FOLLOWING STEP SAY IT! ======
setlocal EnableDelayedExpansion

color 0b
echo ...
set currentPath=%cd%

for /f "tokens=*" %%a in ('time/t') do set now=%%a
set ymd=%date:~0,4%%date:~5,2%%date:~8,2%
set hms=%now:~0,2%%now:~3%%time:~6,2%

if "%ymd%" equ "" ( set ymd=00 )
if "%ymd%" equ "" ( set hms=00 )
set StartTime=%ymd%_%hms%
echo %StartTime%

:StartScreen

rem echo 等待您插入手机
adb wait-for-device

echo.
echo.--------------------------------------------------------
echo.             -     选择类型    -
echo.             x  -  退出脚本
echo.             i  -  安装所有应用
echo.             拖入一个APK文件也可以
echo.             输入apk所在的文件夹亦可
echo.--------------------------------------------------------
echo.

set SelectId=
set /p SelectId=输入:

if "%SelectId%" equ "" ( set SelectId=0&  goto startGetInfo )
if /i "%SelectId%"=="x" ( goto :eof )
if /i "%SelectId%"=="i" ( goto installApks )

echo %selectId%|findstr /c:".apk" && ( echo 拖入了一个APK文件 & goto dragAction )
dir /ad %selectId% >nul 2>nul && (
	echo 有效的文件夹路径，开始安装文件夹 %selectId% 下的apk文件 & goto newApksPath ) || (
			echo 路径无效,请重新选择 & goto StartScreen 
	) 
)

goto StartScreen
rem ============================================================================================
:dragAction
set ApksPath=%selectId%

if exist %ApksPath%  (
	for %%i in ( "%ApksPath%" ) do ( 
		set dragApk=%%~nxi & echo 即将开始安装apk:!dragApk!
		adb install "%ApksPath%"|findstr "^Failure*" && ( echo apk:!dragApk!安装失败 ) || ( echo 安装成功 )
	)
)

goto StartScreen


rem ============================================================================================

:newApksPath

echo 开始安装...
set ApksPath=%selectId%

if not exist "%ApksPath%\*.apk" ( echo 当前目录下无apk文件，请重新输入路径 & goto StartScreen )

set ErrorLogFileName=%ApksPath%\InstallApkErrorLog.txt
set InstallApksLogFile=%ApksPath%\InstallApksLogFile.txt
set InstallSuccessApksList=%ApksPath%\InstallSuccessApksList.txt

goto startInstallApk

rem ============================================================================================

:installApks
echo 开始安装...
set ApksPath="<span style="color:#ff0000;">%currentPath%\apks</span>"
set ErrorLogFileName=%ApksPath%\InstallApkErrorLog.txt
set InstallApksLogFile=%ApksPath%\InstallApksLogFile.txt
set InstallSuccessApksList=%ApksPath%\InstallSuccessApksList.txt

:startInstallApk

echo 安装中...
set /a var=1
set installFlag=0

for /f "delims=" %%i in ('dir/b/a-d %ApksPath%\*.apk') do (
	echo 正在安装第 !var! 个应用: %%i
	set installFlag=success
	adb install "%ApksPath%\%%i"|findstr "^Failure*" && ( 
		set installFlag=faild& echo 第!var!个apk:%%i 安装失败 >> %ErrorLogFileName% ) || (
			echo 安装成功
			for /f "delims=" %%a in ( 'aapt d badging "%ApksPath%\%%i"^|findstr "package: "') do (
				echo %%a
				for /f "delims=' tokens=2" %%b in ( "%%a" ) do (
					echo %%b>>%InstallSuccessApksList%
					rem 安装成功的应用导入文件中
				)
			)
		)
	)
	
	set /a var+=1
	echo !installFlag!_%%i>>%InstallApksLogFile%
)

echo 完成

goto StartScreen